package com.gao.security.autoconfig.verificationcode;

import com.gao.security.autoconfig.event.ValidateCodeRemoveListener;
import com.gao.security.autoconfig.verificationcode.properties.RegisterUrlService;
import com.gao.security.autoconfig.verificationcode.properties.ValidateCodeProperties;
import com.gao.security.verificationcode.storage.InMemoryVerificationCodeOperationManager;
import com.gao.security.verificationcode.storage.VerificationCodeStorageManager;
import com.gao.security.verificationcode.web.verificate.VerificationCodeManager;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-08-01 9:47
 */
@Configuration
public class VerificationCodeAutoConfiguration {


    /**
     * 注册VerificationCodeGenerationStrategyConfig
     * 包装了各类型的操作类
     */
    @Bean
    @ConditionalOnMissingBean
    public RegisterUrlService registerUrlService(ValidateCodeProperties properties) {

        return new RegisterUrlServiceImpl(properties);
    }
    @Bean
    @ConditionalOnMissingBean
    public VerificationCodeManager verificationCodeManager() {
        DefaultVerificationCodeManager defaultVerificationCodeManager = new DefaultVerificationCodeManager();
        defaultVerificationCodeManager.setOperationManager(verificationCodeStorageManager());
        return defaultVerificationCodeManager;
    }

    @Bean
    @ConditionalOnMissingBean
    public VerificationCodeStorageManager verificationCodeStorageManager() {
        return new InMemoryVerificationCodeOperationManager();
    }
    @Bean
    @ConditionalOnMissingBean
    public ValidateCodeRemoveListener validateCodeRemoveListener() {
        return new ValidateCodeRemoveListener(verificationCodeStorageManager());
    }

    /**
     * 内置拦截路径实现类
     */
    private  class RegisterUrlServiceImpl implements RegisterUrlService{

        private  ValidateCodeProperties properties;

        private RegisterUrlServiceImpl(ValidateCodeProperties properties) {
            this.properties = properties;
        }

        @Override
        public List<String> getSendUrl() {
            List<String> list = Lists.newArrayList();
            list.add(properties.getImg().getSendUrl());
            list.add(properties.getSms().getSendUrl());
            list.add(properties.getEmail().getSendUrl());
            return list;
        }

        @Override
        public List<String> getCheckUrl() {
            List<String> list = Lists.newArrayList();
            list.add(properties.getImg().getLoginUrl());
            list.add(properties.getSms().getLoginUrl());
            list.add(properties.getEmail().getLoginUrl());
            return list;
        }
    }
}
